// var firstMenu = document.getElementsByClassName('firstMenu');
// for (let i in firstMenu) {
//     firstMenu[i].onclick = function () {

//         let ul = firstMenu[i].querySelector('ul');
//         ul.classList.add("actived");
//         let len = ul.children.length;
//         console.log(len);
//         creatAnimation({
//             from: 0,
//             to: len * 30,
//             totalMs: 500,
//             onmove: function (n) {
//                 ul.style.height = n + "px";
//             },
//         })
//     }
// }

var titles = document.querySelectorAll('.firstMenu h2');
console.log(titles)
for (let i in titles) {
    titles[i].onclick = function () {
        var beforeOpened = document.querySelector(".submenu[status=opened]");
        /* 将所有已开的全部关闭 */
        if (beforeOpened) {
            closeSubmenu(beforeOpened)
        }
        toggleSubmenu(this.nextElementSibling)
    }
}

function openSubmenu(submenu) {
    var status = submenu.getAttribute("status");
    if (status !== 'closed' && status) {
        return;
    }
    submenu.setAttribute('status', 'playing');
    creatAnimation({
        from: 0,
        to: submenu.children.length * 30,
        totalMs: 500,
        onmove: function (n) {
            submenu.style.height = n + "px";
        },
        onend: function () {
            submenu.setAttribute('status', 'opened')
        }
    })
}

function closeSubmenu(submenu) {

    var status = submenu.getAttribute('status');
    if (status !== "opened") {
        return;
    }
    submenu.setAttribute('status', 'playing');
    creatAnimation({
        from: submenu.children.length * 30,
        to: 0,
        totalMs: 500,
        onmove: function (n) {
            submenu.style.height = n + "px";
        },
        onend: function () {
            submenu.setAttribute('status', 'closed')
        }
    })
}

function toggleSubmenu(submenu) {
    var status = submenu.getAttribute('status');
    if (status === 'playing') {
        return;
    } else if (status === "opened") {
        closeSubmenu(submenu);
    } else {
        openSubmenu(submenu)
    }
}